package com.sunway.resource2es.controller.dto;

import com.sunway.annotations.ddss.DbName;
import com.sunway.exception.built.BusinessException;
import com.sunway.utils.time.TimeUtil;
import com.sunway.utils.time.enums.TimeFormatEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import org.apache.commons.lang3.StringUtils;

/**
 * 数据添加到ES
 *
 * @author <a href="https://tannn.cn/">tan</a>
 * @date 2023/9/15 12:00
 */
@Schema(description = "数据添加到ES")
public class InitIndexDTO {

    /**
     * 数据源名称
     */
    @Schema(description = "数据源名称(147_ruc_2025)", requiredMode = Schema.RequiredMode.REQUIRED)
    @NotBlank(message = "数据源名称")
    String dbName;
    /**
     * 产品代码
     */
    @Schema(description = "数据库表命", requiredMode = Schema.RequiredMode.REQUIRED)
    @NotBlank(message = "数据库表命不能为空")
    String tableName;

    /**
     * 数据表限制条件，where 后面的语句
     */
    @Schema(description = "数据表限制条件，where 后面的语句")
    String condition;

    /**
     * mapping文件名称，含.json后缀，且格式必须是json
     */
    @Schema(description = "mapping文件名称，含.json后缀，且格式必须是json")
    String mappingFileName;

    /**
     * 映射规则字符串
     */
    @Schema(description = "映射规则字符串")
    String mappingStr;

    /**
     * 索引名称
     */
    @Schema(description = "索引名称")
    String indexName;

    /**
     * 索引昵称不能为空
     */
    @Schema(description = "索引别称不能为空", requiredMode = Schema.RequiredMode.REQUIRED)
    @NotBlank(message = "索引别称不能为空")
    String indexAlias;


    public @NotBlank(message = "数据源名称") String getDbName() {
        return dbName;
    }

    public void setDbName(@NotBlank(message = "数据源名称") String dbName) {
        this.dbName = dbName;
    }

    public @NotBlank(message = "数据库表命不能为空") String getTableName() {
        return tableName;
    }

    public void setTableName(@NotBlank(message = "数据库表命不能为空") String tableName) {
        this.tableName = tableName;
    }

    public String getCondition() {
        return condition;
    }

    public void setCondition(String condition) {
        this.condition = condition;
    }

    public String getMappingFileName() {
        return mappingFileName;
    }

    public void setMappingFileName(String mappingFileName) {
        this.mappingFileName = mappingFileName;
    }

    public String getMappingStr() {
        return mappingStr;
    }

    public void setMappingStr(String mappingStr) {
        this.mappingStr = mappingStr;
    }

    public String getIndexName() {
        return indexName;
    }

    public void setIndexName(String indexName) {
        this.indexName = indexName;
    }

    public @NotBlank(message = "索引别称不能为空") String getIndexAlias() {
        return indexAlias;
    }

    public void setIndexAlias(@NotBlank(message = "索引别称不能为空") String indexAlias) {
        this.indexAlias = indexAlias;
    }

    @Override
    public String toString() {
        return "InitIndexDTO{" +
                "dbName='" + dbName + '\'' +
                ", tableName='" + tableName + '\'' +
                ", condition='" + condition + '\'' +
                ", mappingFileName='" + mappingFileName + '\'' +
                ", mappingStr='" + mappingStr + '\'' +
                ", indexName='" + indexName + '\'' +
                ", indexAlias='" + indexAlias + '\'' +
                '}';
    }

    public void indexNameExit() {
        if (StringUtils.isBlank(indexName)) {
            String thisNow = TimeUtil.thisNow(TimeFormatEnum.PURE_FORMAT_DATETIME_DAY);
            indexName = indexAlias + "_" + thisNow;
        }
    }

    public void verifyMapping() {
        if (StringUtils.isBlank(mappingFileName) && StringUtils.isBlank(mappingStr)) {
            throw new BusinessException("mapping文件名称、映射规则字符串,不能均为空，至少有一个值！");
        }
    }

}
